package com.jianyun.jyzs.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.jrmf360.rylib.common.util.ScreenUtil;


/**
 * Created by mlli on 2017/11/9.
 */

public class WaveViews extends View{
    private Paint backgroundPaint;
    private Paint firstWavePaint;//画笔
    private Path firstPath;//画板
    private int angle;//角度
    private Canvas bitmapCanvas;

    private int backgroundColor = Color.BLACK;
    private int firstWaveColor = Color.RED;
    /**
     * 控件尺寸
     */
    private int waveSize = ScreenUtil.getScreenWidth(getContext());
    /**
     * 水位高度
     */
    private float waterProgressMax = 60;
    private float waterProgress = 0;
    /**
     * 振幅
     */
    private int amplitude = dpToPx(20);
    /**
     * 角速度
     */
    private static final float palstance = 0.5F;


    public WaveViews(Context context) {
        super(context);
        init();
    }

    public WaveViews(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WaveViews(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setColor(backgroundColor);
        backgroundPaint.setAntiAlias(true);
        firstPath = new Path();
        /*初始化波浪画笔*/
        firstWavePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        firstWavePaint.setAntiAlias(true);
        firstWavePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        firstWavePaint.setColor(firstWaveColor);
        firstWavePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        bitmapCanvas = new Canvas();
        bitmapCanvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
        canvas.drawRect(0,100,waveSize,0,backgroundPaint);
        /**水位线*/
        float waterLine = (waterProgressMax - waterProgress) * waveSize * 0.01F;
        firstPath.moveTo(0, waterLine);
        int x1 = 0;
        int y1 = 0;
        for (int i = 0; i < waveSize; i++) {
            x1 = i;
            y1 = (int) (amplitude * Math.sin((i * palstance + angle) * Math.PI / 180) + waterLine);
            firstPath.quadTo(x1, y1, x1 + 1, y1);
        }
        firstPath.lineTo(waveSize, waveSize);
        firstPath.lineTo(0, waveSize);
        firstPath.close();
        bitmapCanvas.drawPath(firstPath, firstWavePaint);
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
    }

    private int dpToPx(float dp) {
        return (int) (dp * getContext().getResources().getDisplayMetrics().density);
    }
}
